﻿using System;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace SHomeWorkshop.LunarMind.Adorners
{
    public class TitleQuestionTooltipAdorner : Adorner
    {
        // Be sure to call the base class constructor.
        /// <summary>
        /// 用以表示标题被选取状态的装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入标题的“MainBorder”主子元素。</param>
        public TitleQuestionTooltipAdorner(UIElement adornedElement, Title masterTitle)
            : base(adornedElement)
        {
            this.masterTitle = masterTitle;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

            this.MouseLeave += TitleSelectedAdorner_MouseLeave;
            ToolTip = "有试题文本";
        }

        void TitleSelectedAdorner_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.InvalidateVisual();
        }

        private Title masterTitle;

        public Title MasterTitle
        {
            get { return masterTitle; }
        }

        // A common way to implement an adorner's rendering behavior is to override the OnRender
        // method, which is called by the layout system as part of a rendering pass.
        protected override void OnRender(DrawingContext drawingContext)
        {
            if (masterTitle == null) return;

            if (string.IsNullOrEmpty(masterTitle.QuestionText)) return;

            Rect adornedElementRect = new Rect(this.AdornedElement.RenderSize);

            drawingContext.DrawRectangle(Brushes.Red, new Pen(Brushes.Red, 1), new Rect(adornedElementRect.Left, adornedElementRect.Top - 1, 3, 5));
        }
    }
}
